package com.tarzan.edu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tarzan.common.core.base.BaseResponse;
import com.tarzan.common.core.exception.BusinessException;
import com.tarzan.edu.entity.EduCourse;
import com.tarzan.edu.entity.request.EduCourseIdReq;
import com.tarzan.edu.entity.request.EduCourseReq;
import com.tarzan.edu.entity.response.CoursePublishVo;
import com.tarzan.edu.entity.response.EduCourseResp;
import com.tarzan.edu.service.IEduCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("edu/course")
public class EduCourseController {



    @Autowired
    private IEduCourseService iEduCourseService;



    /** @title insert
     * @description TODO 新增课程信息
     * @param eduCourse
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:52
     */
    @PostMapping(value = "insert",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse<String> insert(@RequestBody EduCourse eduCourse){
        BaseResponse<String> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iEduCourseService.insert(eduCourse));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

    /** @title update
     * @description TODO 修改课程信息
     * @param eduCourse
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 15:59
     */
    @PostMapping(value = "update",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse update(@RequestBody EduCourse eduCourse){
        try{
            iEduCourseService.update(eduCourse);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }

    /** @title delete
     * @description TODO 删除课程信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @author liujinshan
     * @data 2021/5/21 16:01
     */
    @DeleteMapping(value = "delete/{id}")
    public BaseResponse delete(@PathVariable String id){
        try{
            iEduCourseService.delete(id);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }


    /**
     * @description 获取讲师信息列表并分页
     * @author liujinshan
     * param eduTeacherReq
     * @return com.tarzan.common.core.base.BaseResponse
     * @throws
     * @date 2021-5-23 14:43
     */
    @PostMapping(value = "findCoursePageInfo")
    public BaseResponse<IPage<EduCourseResp>> findCoursePageInfo(@RequestBody EduCourseReq eduCourseReq){
        BaseResponse<IPage<EduCourseResp>> response = new BaseResponse<>();
        try{
            response = BaseResponse.ok(iEduCourseService.findCoursePageInfo(eduCourseReq));
        } catch (BusinessException e){
            response.setErrCode(e.getCode());
            response.setErrMsg(e.getMsg());
        } catch (Exception e){
            response.setErrCode("500");
            response.setErrMsg(e.getMessage());
        }
        return response;
    }

    /**
     * @description 根据id查询课程相细信息
     * @author liujinshan
     * param id
     * @return
     * @throws
     * @date 2021-5-23 14:51
     */
    @GetMapping(value = "findCourseById/{id}")
    public BaseResponse<EduCourseResp> findCourseById(@PathVariable String id){
        BaseResponse<EduCourseResp> response;
        try{
            response = BaseResponse.ok(iEduCourseService.findCourseById(id));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }


    /**
     * @title findCoursePublishById
     * @description TODO 根据课程id 查询课程相关信息
     * @param id
     * @return com.tarzan.common.core.base.BaseResponse<com.tarzan.edu.entity.response.CoursePublishVo>
     * @author liujinshan
     * @data 2021/8/17 10:44
     */
    @GetMapping(value = "findCoursePublishById/{id}")
    public BaseResponse<CoursePublishVo> findCoursePublishById(@PathVariable String id){
        BaseResponse<CoursePublishVo> response;
        try{
            response = BaseResponse.ok(iEduCourseService.findCoursePublishById(id));
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return response;
    }

    /**
     * @title publishCourse
     * @description TODO 发布课程
     * @param req
     * @return com.tarzan.common.core.base.BaseResponse
     * @author liujinshan
     * @data 2021/8/17 10:55
     */
    @PostMapping(value = "publishCourse",produces = {"application/json"},consumes = {"application/json"})
    public BaseResponse publishCourse(@RequestBody EduCourseIdReq req){
        try{
            iEduCourseService.publishCourse(req);
        } catch (BusinessException e){
            return BaseResponse.error(e.getCode(),e.getMsg());
        } catch (Exception e){
            return BaseResponse.error("500",e.getMessage());
        }
        return BaseResponse.ok();
    }
}
